package com.ruoyi.gbrz.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.gbrz.domain.GbrzFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.gbrz.domain.Gbrz;
import com.ruoyi.gbrz.service.IGbrzService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 贯标认证Controller
 *
 * @author huacai
 * @date 2024-10-26
 */
@RestController
@RequestMapping("/gbrz/gbrz")
public class GbrzController extends BaseController {
    @Resource
    private IGbrzService gbrzService;

    /**
     * 查询贯标认证列表
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:list')")
    @GetMapping("/list")
    public TableDataInfo list(Gbrz gbrz) {
        startPage();
        List<Gbrz> list = gbrzService.selectGbrzList(gbrz);
        return getDataTable(list);
    }

    /**
     * 导出贯标认证列表
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:export')")
    @Log(title = "贯标认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gbrz gbrz) {
        List<Gbrz> list = gbrzService.selectGbrzList(gbrz);
        ExcelUtil<Gbrz> util = new ExcelUtil<Gbrz>(Gbrz.class);
        util.exportExcel(response, list, "贯标认证数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Gbrz> util = new ExcelUtil<Gbrz>(Gbrz.class);
        util.importTemplateExcel(response, "贯标认证数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "贯标认证", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Gbrz> util = new ExcelUtil<Gbrz>(Gbrz.class);
        InputStream inputStream = file.getInputStream();
        List<Gbrz> list = util.importExcel(inputStream);
        inputStream.close();
        int count = gbrzService.batchInsertGbrz(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取贯标认证详细信息
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:query')")
    @GetMapping(value = "/{gbrzId}")
    public AjaxResult getInfo(@PathVariable("gbrzId") String gbrzId) {
        return success(gbrzService.selectGbrzByGbrzId(gbrzId));
    }

    /**
     * 新增贯标认证
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:add')")
    @Log(title = "贯标认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gbrz gbrz) {
        gbrz.setUserId(Math.toIntExact(getUserId())); //用户ID
        gbrz.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        gbrz.setCreateBy(getUsername()); //创建人
        return toAjax(gbrzService.insertGbrz(gbrz));
    }

    /**
     * 修改贯标认证
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:edit')")
    @Log(title = "贯标认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gbrz gbrz) {
        return toAjax(gbrzService.updateGbrz(gbrz));
    }

    /**
     * 删除贯标认证
     */
    @PreAuthorize("@ss.hasPermi('gbrz:gbrz:remove')")
    @Log(title = "贯标认证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gbrzIds}")
    public AjaxResult remove(@PathVariable String[] gbrzIds) {
        return toAjax(gbrzService.deleteGbrzByGbrzIds(gbrzIds));
    }

    /**
     * 获得贯标认证主键ID (UUID)
     */
    @GetMapping("/getGbRzId")
    public AjaxResult getGbRzId(){
        String getGbRzId = IdUtils.fastSimpleUUID();
        return success(getGbRzId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertGbrzFjByGbrzId(@RequestBody GbrzFj gbrzFj){
        return toAjax(gbrzService.insertGbrzFjByGbrzId(gbrzFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectGbrzFjList(String sId){
        GbrzFj gbrzFj = new GbrzFj();
        gbrzFj.setGbrzId(sId);
        return AjaxResult.success(gbrzService.selectGbrzFjList(gbrzFj));
    }

    /**
     * 删除贯标认证附件
     */
    @Log(title = "贯标认证附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteGbrzFjByFjIds(@PathVariable Integer[] fjIds)
    {
        return toAjax(gbrzService.deleteGbrzFjByFjIds(fjIds));
    }

}
